package com.mt.api.modules.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mt.api.constants.constant.Constants;
import com.mt.api.constants.enums.OrderStatusEnum;
import com.mt.api.modules.domain.AppNews;
import com.mt.api.modules.domain.AppOrder;
import com.mt.api.modules.domain.Instore;
import com.mt.api.modules.domain.dto.CustomerBillStatDto;
import com.mt.api.modules.domain.dto.StockWithPrice;
import com.mt.api.modules.domain.vo.IndexStatDataVo;
import com.mt.api.modules.manager.ICustomerBillManager;
import com.mt.api.modules.manager.IInstoreManager;
import com.mt.api.modules.manager.IOrderManager;
import com.mt.api.modules.manager.IStockManager;
import com.mt.api.modules.mapper.AppNewsMapper;
import com.mt.api.modules.service.AppIndexService;
import com.mt.api.util.AmountUtil;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: plover
 * @Date: 2020/07/16/1:09 下午
 * @Description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppIndexServiceImpl implements AppIndexService {

    private final AppNewsMapper appNewsMapper;
    private final IOrderManager orderManager;
    private final IInstoreManager instoreManager;
    private final IStockManager stockManager;
    private final ICustomerBillManager customerBillManager;


    @Override
    public WebResponse<IndexStatDataVo> getStatisticsData() {
        IndexStatDataVo indexStatDataVo = new IndexStatDataVo();
        String todayDate = DateUtil.today();

        // 出库销售单
        List<AppOrder> orderList = orderManager.getOrderByDate(Arrays.asList(OrderStatusEnum.PART_OUT_STORE.getCode(), OrderStatusEnum.COMPLETE_OUT_STORE.getCode()), todayDate);
        if (CollectionUtils.isNotEmpty(orderList)) {
            int amount = orderList.stream().mapToInt(AppOrder::getAmount).sum();
            int qty = orderList.stream().mapToInt(AppOrder::getOutStoreQty).sum();
            indexStatDataVo.setDayOrderAmount(AmountUtil.divideHundred(amount));
            indexStatDataVo.setDayOrderQty(qty);
            indexStatDataVo.setDayOrderCount(orderList.size());
        }

        // 查询所有客户账单
        List<CustomerBillStatDto> billStatList = customerBillManager.getCustomerBillStat(null);
        if (CollectionUtils.isNotEmpty(billStatList)) {
            Integer unSettleAmount = billStatList.stream().mapToInt(bill -> bill.getUnSettleAmount()).sum();
            indexStatDataVo.setUnSettleAmount(AmountUtil.divideHundred(unSettleAmount));
        }

        // 入库数据
        List<Instore> instoreList = instoreManager.getInstoreByDate(Constants.InstoreType.PRODUCTION, todayDate);
        if (CollectionUtils.isNotEmpty(instoreList)) {
            int qty = instoreList.stream().mapToInt(Instore::getQty).sum();
            indexStatDataVo.setDayInstoreQty(qty);
            indexStatDataVo.setDayInstoreCount(instoreList.size());
        }

        // 总库存数量
        List<StockWithPrice> stockWithPriceList = stockManager.getStockWithPrice();
        if (CollectionUtils.isNotEmpty(stockWithPriceList)) {
            int qty = stockWithPriceList.stream().mapToInt(StockWithPrice::getQty).sum();
            int amount = stockWithPriceList.stream().mapToInt(stock -> stock.getQty() * stock.getWholesalePrice()).sum();
            indexStatDataVo.setTotalStockQty(qty);
            indexStatDataVo.setTotalStockAmount(AmountUtil.divideHundred(amount));
        }
        return WebResponseUtils.success(indexStatDataVo);
    }

    @Override
    public WebResponse getNotice() {
        List<AppNews> appNews = appNewsMapper.selectList(Wrappers.<AppNews>lambdaQuery()
                .orderByDesc(AppNews::getId)
                .last("limit 3"));
        StringBuilder content = new StringBuilder();
        appNews.forEach(appNews1 -> {
            content.append(appNews1.getContent()).append(";");
        });
        return WebResponseUtils.successJSONObject("noticeContent", content.toString());
    }

}
